package com.yh.hljapp.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yh.hljapp.AppConfig;
import com.yh.hljapp.AppContext;
import com.yh.hljapp.R;
import com.yh.hljapp.api.ErrorHandler;
import com.yh.hljapp.api.RetrofitSingleton;
import com.yh.hljapp.base.BaseFragment;
import com.yh.hljapp.common.FullyLinearLayoutManager;
import com.yh.hljapp.common.MyScrollview;
import com.yh.hljapp.common.PLog;
import com.yh.hljapp.common.StringUtils;
import com.yh.hljapp.common.TDevice;
import com.yh.hljapp.common.UIHelper;
import com.yh.hljapp.component.dialog.DialogHelp;
import com.yh.hljapp.gddb.gbmanager.AddSale_Manager;
import com.yh.hljapp.gddb.gdbean.AddCbObj_HeadBean;
import com.yh.hljapp.gddb.gdbean.AddSaleInt_body_InOutList_Bean;
import com.yh.hljapp.gddb.gdbean.Add_Sync_Bean;
import com.yh.hljapp.gddb.gdbean.Add_bodyOut_Bean;
import com.yh.hljapp.modules.adatper.Add_Pro_pick_Adapter;
import com.yh.hljapp.modules.adatper.Add_Saleout_Adapter;
import com.yh.hljapp.modules.bean.AddCB_Obj_HB_Bean;
import com.yh.hljapp.modules.bean.AddCb_obj_bodyBean;
import com.yh.hljapp.modules.bean.CM;
import com.yh.hljapp.modules.bean.CustPhone;
import com.yh.hljapp.modules.bean.Result;
import com.yh.hljapp.modules.bean.SimpleBackPage;
import com.yh.hljapp.modules.bean.User;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static android.R.attr.name;
import static com.yh.hljapp.AppConfig.SCN_CUST_ACTION_SCODE;
import static com.yh.hljapp.R.id.ll;
import static com.yh.hljapp.api.RetrofitSingleton.context;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：zhanghang
 * <p>
 * 工程：hlj
 * <p>
 * 文件名：ADDSaleIntFragment
 * <p>
 * 创建时间：2017.07.31.11:40
 */

public class ADDPro_pickFragment extends BaseFragment implements Add_Saleout_Adapter.OnSaleAddAdapterListern, Add_Pro_pick_Adapter.OnSaleAddAdapterListern {

    @InjectView(R.id.tv_gh_unit)
    TextView tvGhUnit;
    @InjectView(R.id.ll_gh_unit)
    LinearLayout llGhUnit;
    @InjectView(R.id.tv_zdr)
    TextView tvZdr;
    @InjectView(R.id.ll_zdr)
    LinearLayout llZdr;
    @InjectView(R.id.tv_fh)
    TextView tvFh;
    @InjectView(R.id.ll_fh)
    LinearLayout llFh;
    @InjectView(R.id.tv_ywy)
    TextView tvYwy;
    @InjectView(R.id.ll_ywy)
    LinearLayout llYwy;
    @InjectView(R.id.tv_bg)
    TextView tvBg;
    @InjectView(R.id.ll_bg)
    LinearLayout llBg;
    @InjectView(R.id.tv_sh_warehouse)
    TextView tvShWarehouse;
    @InjectView(R.id.ll_sh_warehouse)
    LinearLayout llShWarehouse;
    @InjectView(R.id.recyclerview)
    RecyclerView mRecycleview;
    @InjectView(R.id.scrollView)
    MyScrollview scrollView;
    @InjectView(R.id.tv_ll_type)
    TextView tvLlType;
    @InjectView(R.id.ll_ll_type)
    LinearLayout llLlType;
    @InjectView(R.id.ll_content)
    LinearLayout ll_content;
    @InjectView(R.id.tv_all)
    TextView tv_all;

    private boolean isSave;
    private User mUser;
    private Add_Pro_pick_Adapter mAdapter;
    ArrayList<Add_bodyOut_Bean> checkedCKS;
    private AddCb_obj_bodyBean mBodyBean;
    private String currBillNumb;
    private int count;
    private List<String> mFbillNumberList = new ArrayList<>();

    private AddCbObj_HeadBean addCbObj_HeadBean;
    private String type = "1";
    private int position;
    private String mPicktvShWarehouses;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };
    private BroadcastReceiver mSamDataReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(SCN_CUST_ACTION_SCODE)) {
                String message = intent.getStringExtra(AppConfig.SCN_CUST_EX_SCODE);
                JSONObject jo = null;
                try {
                    jo = new JSONObject(message);
                    final String fBase = jo.optString("FBase");
                    final String fWeight = jo.optString("FWeight");
                    String fBillNumber = jo.optString("FBillNumber");
                    currBillNumb = fBillNumber;
                    mFbillNumberList.add(fBillNumber);

                    User currentUser = AppContext.getInstance().getCurrentUser();
                    if (currentUser == null) {
                        Snackbar.make(mActivity.getWindow().getDecorView(), "当前用户为空，请重新登陆", Snackbar.LENGTH_SHORT).show();
                    } else {
                        handleOtherText(fBase, fWeight);
                    }
                } catch (JSONException e) {
                    Snackbar.make(mActivity.getWindow().getDecorView(), "请扫描正确二维码!", Snackbar.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
        }
    };

    private void handleOtherText(String text, String fWeight) {
        //先去判断扫描的单号是否存在
        boolean matExist = AddSale_Manager.getInstance(mActivity).isMatExist(text);
        if (matExist) {//存在
            onlyMatInsert(text, fWeight);
        } else {//不存在
            Snackbar.make(mActivity.getWindow().getDecorView(), "出库单不匹配，请重新扫描或同步数据！", Snackbar.LENGTH_SHORT).show();
        }
    }

    public int getHeight(View view) {// 获得某组件的高度
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        return view.getMeasuredHeight();
    }

    //扫描条码只对应一个物料时的添加流程
    private void onlyMatInsert(String barcode, String fWeight) {

        //先判断已添加的中是否存在
        List<Add_bodyOut_Bean> listMatBean = mAdapter.getBackList();
        for (int i = 0; i < listMatBean.size(); i++) {
            Add_bodyOut_Bean addSyncBean = listMatBean.get(i);
            String id = addSyncBean.getFBillNum();
            //判断是否添加一样
            if (currBillNumb.equals(id)) {
                Snackbar.make(mActivity.getWindow().getDecorView(), "请确认该产品是否已经添加！", Snackbar.LENGTH_SHORT).show();
                return;
            }
        }
        //设置数据
        Add_Sync_Bean matById = AddSale_Manager.getInstance(mActivity).getMatById(barcode);
        String model = matById.getModel();
        Add_bodyOut_Bean addSyncBean = new Add_bodyOut_Bean();

        addSyncBean.setUnit_id(matById.getUnit_id());
        addSyncBean.setId(matById.getId());
        addSyncBean.setCount(fWeight);
        addSyncBean.setModel(matById.getModel());
        addSyncBean.setName(matById.getName());
        addSyncBean.setNumber(matById.getNumber());
        addSyncBean.setUnitgroup_name(matById.getUnitgroup_name());
        addSyncBean.setUnit_name(matById.getUnit_name());
        addSyncBean.setDefunitId(matById.getDefunitId());
        addSyncBean.setBaseName(matById.getBaseName());
        addSyncBean.setBaseunit(matById.getBaseunit());
        addSyncBean.setUnitgroup_id(matById.getUnitgroup_id());
        addSyncBean.setFBillNum(currBillNumb);

        String ffStockIDName = addCbObj_HeadBean.getFFStockIDName();
        if (ffStockIDName != null) {
            addSyncBean.setFDCStockName(addCbObj_HeadBean.getFFStockIDName());
        } else {
            addSyncBean.setFDCStockName(mPicktvShWarehouses);
        }

        addSyncBean.setFDCStockID(addCbObj_HeadBean.getFFStockID());

        checkedCKS.add(addSyncBean);

        count += Integer.parseInt(fWeight);
        tv_all.setText("" + count);
        TextView mTextView = new TextView(mActivity);
        mTextView.setTextSize(25);
        mTextView.setTextColor(Color.BLACK);
        mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
        int childCount = ll_content.getChildCount();
        boolean haseSame = false;
        int pos = -1;
        if (childCount == 0) {
            mTextView.setText(model + " 重量:" + fWeight);
            ll_content.addView(mTextView);
        } else {
            for (int i = 0; i < childCount; i++) {
                String s = ((TextView) ll_content.getChildAt(i)).getText().toString();
                if (s.contains(model)) {
//                ll_content.addView(mTextView);
                    pos = i;
                    haseSame = true;
                    break;
//                    ((TextView) ll_content.getChildAt(i)).setText(model + " zzz:" + fWeight);
                } else {
//                    mTextView.setText(model + " www:" + fWeight);
//                    ll_content.addView(mTextView);
                }
            }
            if (haseSame) {
                String text = ((TextView) ll_content.getChildAt(pos)).getText().toString();
                String substring = text.substring(text.indexOf(":") + 1, text.length());
                int i = Integer.parseInt(substring);
                int i1 = Integer.parseInt(fWeight);

                ((TextView) ll_content.getChildAt(pos)).setText(model + " 重量:" + (i + i1));
            } else {
                mTextView.setText(model + " 重量:" + fWeight);
                ll_content.addView(mTextView);
            }
        }

        mAdapter = new Add_Pro_pick_Adapter(mActivity, checkedCKS, this);
        mRecycleview.setAdapter(mAdapter);
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = View.inflate(mActivity, R.layout.fragment_cbobj, null);
        ButterKnife.inject(this, view);
        initView(view);
        initData();
        return view;
    }


    @Override
    public void initView(View view) {
        super.initView(view);
        TDevice.hideSoftKeyboard(mActivity);
        setHasOptionsMenu(true);
        IntentFilter intentFilter = new IntentFilter(SCN_CUST_ACTION_SCODE);
        mActivity.registerReceiver(mSamDataReceiver, intentFilter);
        llGhUnit.setOnClickListener(this);
        llZdr.setOnClickListener(this);
        llBg.setOnClickListener(this);
        llYwy.setOnClickListener(this);
        llShWarehouse.setOnClickListener(this);
        llFh.setOnClickListener(this);
        llLlType.setOnClickListener(this);
        checkedCKS = new ArrayList<>();

        isSave = false;

        FullyLinearLayoutManager linearLayoutManager = new FullyLinearLayoutManager(getContext());
        mRecycleview.setNestedScrollingEnabled(false);
        //设置布局管理器
        mRecycleview.setLayoutManager(linearLayoutManager);

        mAdapter = new Add_Pro_pick_Adapter(mActivity, checkedCKS, this);
    }

    @Override
    public void initData() {
        super.initData();
        mUser = AppContext.getInstance().getCurrentUser();
        addCbObj_HeadBean = new AddCbObj_HeadBean();
        mBodyBean = new AddCb_obj_bodyBean();

        String PicktvGhUnits = AppContext.getInstance().getProperty("PicktvGhUnit");  //领料部门
        String PicktvBgs = AppContext.getInstance().getProperty("PicktvBg");     //领料人
        String PicktvYwys = AppContext.getInstance().getProperty("PicktvYwy");     //发料人
        mPicktvShWarehouses = AppContext.getInstance().getProperty("PicktvShWarehouse");
        //id
        String PicktvGhUnits_id = AppContext.getInstance().getProperty("PicktvGhUnit_id");
        String PicktvBgs_id = AppContext.getInstance().getProperty("PicktvBg_id");
        String PicktvYwys_id = AppContext.getInstance().getProperty("PicktvYwy_id");
        String PicktvShWarehouses_id = AppContext.getInstance().getProperty("PicktvShWarehouse_id");

        if (!StringUtils.isEmpty(PicktvGhUnits) && !StringUtils.isEmpty(PicktvBgs)
                && !StringUtils.isEmpty(PicktvYwys) && !StringUtils.isEmpty(mPicktvShWarehouses)) {

            tvGhUnit.setText(PicktvGhUnits);
            tvBg.setText(PicktvBgs);
            tvYwy.setText(PicktvYwys);
            tvShWarehouse.setText(mPicktvShWarehouses);

            addCbObj_HeadBean.setFFStockID(PicktvShWarehouses_id);
            addCbObj_HeadBean.setFLUser(PicktvBgs_id);
            addCbObj_HeadBean.setFFUser(PicktvYwys_id);
            addCbObj_HeadBean.setFLDeparmentID(PicktvGhUnits_id);
        }


        mAdapter = new Add_Pro_pick_Adapter(mActivity, checkedCKS, this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        Bundle bundle = new Bundle();
        switch (v.getId()) {
            case R.id.ll_gh_unit:
                bundle.putString("type", "bm");        //领料部门
                UIHelper.showSimpleBackForResult(this, mActivity, 1, SimpleBackPage.CUST_PHEON, bundle, "领料部门");
                break;
            case R.id.ll_bg:
                bundle.putString("type", "bg");       //领料人
                UIHelper.showSimpleBackForResult(this, mActivity, 3, SimpleBackPage.CUST_PHEON, bundle, "领料人");
                break;
            case R.id.ll_ywy:
                bundle.putString("type", "ywy");             //发料人
                UIHelper.showSimpleBackForResult(this, mActivity, 5, SimpleBackPage.CUST_PHEON, bundle, "发料人");
                break;
            case R.id.ll_sh_warehouse:
                bundle.putString("type", "fhck");        //发料仓库
                UIHelper.showSimpleBackForResult(this, mActivity, 6, SimpleBackPage.CUST_PHEON, bundle, "发料仓库");
                break;
            case R.id.ll_ll_type:
                AlertDialog dialogHelp = DialogHelp.getSingleChoiceDialog(mActivity, "生产领料方式", new String[]{"蓝字", "红字"}, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == 0) {
                            type = "1";
//                            addCbObj_HeadBean.setFtype(type);//1代表领料，-1是退料
                            tvLlType.setText("蓝字");
                            tvLlType.setTextColor(mActivity.getResources().getColor(R.color.blue));
                            dialog.dismiss();
                        } else {
                            type = "-1";
//                            addCbObj_HeadBean.setFtype(type);
                            tvLlType.setText("红字");
                            tvLlType.setTextColor(mActivity.getResources().getColor(R.color.red));
                            dialog.dismiss();
                        }
                    }
                }).show();
                break;

        }
    }




    @Override
    public void onFHCKClick(Add_bodyOut_Bean bean, int position) {


        this.position = position;
        Bundle bundle = new Bundle();
        bundle.putString("type", "shck");
        UIHelper.showSimpleBackForResult(this, mActivity, 7, SimpleBackPage.CUST_PHEON, bundle, "收料仓库");
    }

    @Override
    public void onDeleted(Add_bodyOut_Bean bean, int position) {
        count -= Integer.parseInt(bean.getCount());
        tv_all.setText("" + count);


        int childCount = ll_content.getChildCount();
        if (childCount >0) {
            for (int i = 0; i < childCount; i++) {
                String s = ((TextView) ll_content.getChildAt(i)).getText().toString();
                if (s.contains(bean.getModel())) {

                    String text = ((TextView) ll_content.getChildAt(i)).getText().toString();
                    String substring = text.substring(text.indexOf(":") + 1, text.length());
                    int a = Integer.parseInt(substring);
                    int i1 = Integer.parseInt(bean.getCount());
                    if ((a-i1)==0){
                        ll_content.removeViewAt(i);
                    }else{
                        ((TextView) ll_content.getChildAt(i)).setText(bean.getModel() + " 重量:" + (a - i1));
                    }
                    break;
                }
            }
        }
    }

    @Override
    public void onDeleteCountClick(Add_bodyOut_Bean bean, int position) {

    }

    @Override
    public void onDeleteClick(int position) {

    }

    @Override
    public void onCB_ObjCKClick(Add_bodyOut_Bean bean, int position) {
        this.position = position;
        Bundle bundle = new Bundle();
        bundle.putString("type", "cbdx");
        UIHelper.showSimpleBackForResult(this, mActivity, 8, SimpleBackPage.CUST_PHEON, bundle, "成本对象");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            final CustPhone item = (CustPhone) data.getSerializableExtra("item");
            if (item != null) {
                String name = item.getName();
                String id = item.getId();
                switch (requestCode) {
                    case 1:
                        tvGhUnit.setText(name);
                        addCbObj_HeadBean.setFLDeparmentID(id);
                        AppContext.getInstance().setProperty("PicktvGhUnit", name);
                        AppContext.getInstance().setProperty("PicktvGhUnit_id", id);
                        break;
                    case 3:
                        tvBg.setText(name);
                        addCbObj_HeadBean.setFLUser(id);
                        AppContext.getInstance().setProperty("PicktvBg", name);
                        AppContext.getInstance().setProperty("PicktvBg_id", id);
                        break;
                    case 5:
                        tvYwy.setText(name);
                        addCbObj_HeadBean.setFFUser(id);
                        AppContext.getInstance().setProperty("PicktvYwy", name);
                        AppContext.getInstance().setProperty("PicktvYwy_id", id);
                        break;
                    case 6:
                        tvShWarehouse.setText(name);
                        addCbObj_HeadBean.setFFStockID(id);
                        addCbObj_HeadBean.setFFStockIDName(name);
                        AppContext.getInstance().setProperty("PicktvShWarehouse", name);
                        AppContext.getInstance().setProperty("PicktvShWarehouse_id", id);
                        break;
                    case 7://adapter中点击发货仓库 自选的仓库
                        ArrayList<Add_bodyOut_Bean> backList = mAdapter.getBackList();
                        Add_bodyOut_Bean addSyncBean = backList.get(position);
                        addSyncBean.setTDCStockID(item.getId());
                        addSyncBean.setTDCStockName(item.getName());
                        backList.set(position, addSyncBean);
                        mAdapter.setBackList(backList);
                        mAdapter.notifyItemChanged(position);
                        break;
                    case 8:
                        mAdapter.setCbObj(name);
                        mBodyBean.setFCbObj(id);
                        mAdapter.notifyItemChanged(position);
                        break;
                }
            }
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.send_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.send_btn:
                subMat();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    //提交
    private void subMat() {
        String type = tvLlType.getText().toString();
        String cbObj = mAdapter.getCbObj();
        ArrayList<Add_bodyOut_Bean> addSyncBeen_List = mAdapter.getBackList();
        if (addSyncBeen_List == null || addSyncBeen_List.size() == 0) {
            Snackbar.make(mActivity.getWindow().getDecorView(), "请扫描物料，再提交", Snackbar.LENGTH_SHORT).show();
            return;
        }
//        if ("蓝字".equals(type)) {
//            if (StringUtils.isEmpty(cbObj)) {
//                Snackbar.make(mActivity.getWindow().getDecorView(), "必填信息不能为空!", Snackbar.LENGTH_SHORT).show();
//                return;
//            }
//        } else {
//        }
        //获取表头
        String fDSupplyID = addCbObj_HeadBean.getFLDeparmentID();
        String fpUser = addCbObj_HeadBean.getFFUser();
        String fLUser = addCbObj_HeadBean.getFLUser();
        String fdcStock = addCbObj_HeadBean.getFFStockID();

        if (mUser != null) {
            String id = mUser.getId();
            addCbObj_HeadBean.setFUser(id);
        } else {
            Snackbar.make(mActivity.getWindow().getDecorView(), "当前用户获取失败，请重新登陆！", Snackbar.LENGTH_SHORT).show();
        }

        if (!StringUtils.isEmpty(fDSupplyID) && !StringUtils.isEmpty(fpUser) && !StringUtils.isEmpty(fdcStock)
                && !StringUtils.isEmpty(fLUser)) {

            //设置表头
            addCbObj_HeadBean.setFLDeparmentID(fDSupplyID);
            addCbObj_HeadBean.setFFUser(fpUser);
            addCbObj_HeadBean.setFLUser(fLUser);
            addCbObj_HeadBean.setFFStockID(fdcStock);
            if ("蓝字".equals(type)){
                type="1";
            }else if ("红字".equals(type)){
                type="-1";
            }
            addCbObj_HeadBean.setFtype(type);


            //表体
            List<AddCb_obj_bodyBean> bodys = new ArrayList<>();
            List<AddSaleInt_body_InOutList_Bean> bodyInOuts = new ArrayList<>();

            for (int i = 0; i < mFbillNumberList.size(); i++) {
                AddSaleInt_body_InOutList_Bean bodyInOutsBean = new AddSaleInt_body_InOutList_Bean();
                bodyInOutsBean.setFText(mFbillNumberList.get(i));
                bodyInOuts.add(bodyInOutsBean);
            }

            for (int i = 0; i < addSyncBeen_List.size(); i++) {

                Add_bodyOut_Bean addSyncBeans = addSyncBeen_List.get(i);
                AddCb_obj_bodyBean body = new AddCb_obj_bodyBean();
                String fCount = addSyncBeans.getCount();
                if (fCount.contains(".")) {
                    String counts = fCount.substring(0, fCount.indexOf("."));
                    body.setFQty(counts);
                } else {
                    body.setFQty(fCount);
                }
                String unit_id = addSyncBeans.getUnit_id();
                String fdcStockID = addSyncBeans.getTDCStockID();  //表体id
                String id = addSyncBeans.getId();

                if (!StringUtils.isEmpty(fdcStockID)) {
                    body.setFStockId(fdcStockID);
                } else if (!StringUtils.isEmpty(fdcStock)) {
                    body.setFStockId(fdcStock);
                } else {
                    body.setFStockId("0");
                }

                body.setFUnitId(unit_id);
                body.setFProductID(id);

                bodys.add(body);
            }

            showWaitDialog();
            AddCB_Obj_HB_Bean addObj_HBBean = new AddCB_Obj_HB_Bean();
            addObj_HBBean.setBodyList(bodys);
            addObj_HBBean.setInoutList(bodyInOuts);
            addObj_HBBean.setHeadDetail(addCbObj_HeadBean);

            Gson gson = new Gson();
            String json = gson.toJson(addObj_HBBean);
            String token = AppContext.getInstance().getProperty(AppConfig.PRIVATE_TOKEN);
            CM cm = new CM();
            cm.setToken(token);
            cm.setData(json);
            RetrofitSingleton.getApiService(mActivity).
                    PickSubmit(cm)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(observer);
        } else {
            Snackbar.make(mActivity.getWindow().getDecorView(), "必填信息不能为空！", Snackbar.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        try {
            mActivity.unregisterReceiver(mSamDataReceiver);
        } catch (Exception ex) {
        }
        ButterKnife.reset(this);
    }


    Observer<Result> observer = new Observer<Result>() {
        @Override
        public void onCompleted() {
            PLog.d("s", "onCompleted");
            hideWaitDialog();
        }

        @Override
        public void onError(Throwable e) {
            PLog.d("s", e.getMessage());
            String errBody = ErrorHandler.handle(e);
            Snackbar.make(getActivity().getWindow().getDecorView(), errBody, Snackbar.LENGTH_SHORT).show();
            hideWaitDialog();
        }

        @Override
        public void onNext(Result result) {
            hideWaitDialog();
            if (result.isSuccess()) {
                Snackbar.make(mActivity.getWindow().getDecorView(), "提交成功!", Snackbar.LENGTH_SHORT).show();
                isSave = true;
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mActivity.finish();
                        Bundle bundle = new Bundle();
                        UIHelper.showSimpleBack(mActivity, SimpleBackPage.ADD_PRO_PICK, bundle, "生产领料");
                    }
                }, 1000);//3秒后执行Runnable中的run方法
//                mActivity.finish();
            } else {
                Snackbar.make(mActivity.getWindow().getDecorView(), result.getMessage(), Snackbar.LENGTH_SHORT).show();
            }
        }
    };

}

